package com.rupertjones.globalcron.server.web;

import com.rupertjones.globalcron.common.domain.AuditLog;
import com.rupertjones.globalcron.common.domain.AuditLogType;
import com.rupertjones.globalcron.server.web.service.AuditLogService;
import com.rupertjones.globalcron.server.web.service.JobService;
import com.rupertjones.globalcron.server.web.service.WebSessionSessionContextProvider;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.Date;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
public class StopAllAction extends AbstractFormAction {

    @SpringBean
    private JobService jobService;

    @SpringBean
    private WebSessionSessionContextProvider contextProvider;

    @SpringBean
    private AuditLogService auditLogService;

    @DefaultHandler
    public Resolution view() {
        jobService.stopAll();
        AuditLog auditLog = new AuditLog();
        auditLog.setMessage("Stop All Jobs");
        auditLog.setUser(contextProvider.getCurrentUser());
        auditLog.setType(AuditLogType.STOP_ALL_JOBS);
        auditLog.setCreatedAt(new Date());
        auditLogService.audit(auditLog);
        return new ForwardResolution(HOME_ACTION);
    }
}
